using System;
using System.IO;
using System.Windows.Forms;
using rlTodayMakeSkin.Project;

namespace rlTodayMakeSkin.GUI
{
    public partial class ProjectPropertiesWindow : Form
    {
        private rlTodayProject prj;
        private char[] invalidChars = { '?', '/', '\\', ':', '*', '|', '\"', '<', '>' };

        public ProjectPropertiesWindow(rlTodayProject p)
        {
            InitializeComponent();
            prj = p;
            InitializeProperties();
            this.ShowDialog();
        }

        private void InitializeProperties()
        {
            txtPrjName.Text = prj.Name;
            txtPrjHeight.Text = prj.Height.ToString();
            txtPath.Text = prj.Path;
            txtRefresh.Text = prj.Refresh.ToString();
            setSkinWidth(prj); //sets the correct value in the cbWidth combo box.
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            /*if (prj.Name != txtPrjName.Text) {
                try
                {
                    Directory.Move(prj.Path, prj.Path.Substring(prj.Path.LastIndexOf("\\") + 1) + txtPrjName.Text);
                }
                catch (Exception ex) { 
                    MessageBox.Show("Error while changing project name:\n" + ex.Message);
                    InitializeProperties();
                    return;
                }
            }*/
            //set the width
            prj.Width = getWidthValue();
            prj.Height = int.Parse(txtPrjHeight.Text);
            prj.Refresh = int.Parse(txtRefresh.Text);
            this.Dispose(); //close project properties window
        }


        private void txtNum_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Convalid height o refresh value inserted in textboxes
            if (! char.IsDigit(e.KeyChar) && e.KeyChar != '\b')
            {
                e.Handled = true; //no show typed char
            }
        }


        private void txt_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Convalid typed directory name
            string s = new String(invalidChars);
            if (s.Contains(e.KeyChar.ToString())) {
                MessageBox.Show("Invalid char typed: non permitted chars are " + s);
                e.Handled = true;  //no show typed char
            }
        }

        private int getWidthValue()
        { //returns the SkinWidth value selected in the combo box cbWidth
            string w = cbWidth.Text;
            if (w.Equals("640"))
                return (int)SkinWidth.VGA;
            else if (w.Equals("320"))
                return (int)SkinWidth.QVGA;
            else
            {//custom value
                return  int.Parse(w);
            }
        }

        private void setSkinWidth(rlTodayProject p)
        {//Sets the correct text in the combo box with the width of
         //passed project
            switch (p.Width)
            {
                case (int)SkinWidth.VGA:
                    cbWidth.SelectedIndex = 0;
                    break;
                case (int)SkinWidth.QVGA:
                    cbWidth.SelectedIndex = 1;
                    break;
                default:
                    int newPos = cbWidth.Items.Add(p.Width.ToString());
                    cbWidth.SelectedIndex = newPos;
                    break;
            }
        }

        private void cbWidth_Leave(object sender, EventArgs e)
        {
            int w = 0;
            if (int.TryParse(cbWidth.Text, out w) == false)
                MessageBox.Show("The skin width must be a number. Please correct the value.", frmMain.TITLE);
        }


    }
}